<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
        integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">


    <link href="css/base.css" rel="stylesheet">
    <link href="css/head.css" rel="stylesheet">

    <link href="css/carousel.css" rel="stylesheet">
</head>

<body>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
        integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js"></script>

    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/head.js"></script>
    <script src="js/Utils.js"></script>
    <script src="js/mock.js"></script>
    <script src="js/mock/carousel.js"></script>

    <div class="head"></div>

    <div class="main">
        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators"></ol>
            <div class="carousel-inner"></div>
            <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>

        <div class="uploadBox" style="width: 30%;margin:auto;margin-top:20px;">
            <div class="button">上传</div>
            <input type="file" id="upload">
        </div>

        <div class="carouselBox">
        </div>
    </div>

    <script>
        $('document').ready(function () {
            $.ajax({
                url: baseUrl + '/carousel.php',
                type: 'GET',
                success: function (res) {
                    if (typeof (res) == 'string')
                        res = JSON.parse(res)

                    if (res.code == 200) {
                        renderCarousel(res.data)
                    }


                }
            })

            initEven()
        })

        function initEven(){
                uploadCarousel()
            }

        function changeCarousel(id){
            $.ajax({
                url : 'http://localhost/change_carousel.php',
                type: 'POST',
                success: function(res){
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)

                    if(res.code == 200){
                        alert(res.msg)
                        location.href="carousel_manage.html"
                    }
                        
                }
            })
        }

        function delCarousel(id){
            let data = {
                id
            }

            data = JSON.stringify(data)

            $.ajax({
                data,
                url : baseUrl + 'del_carousel.php',
                type: 'POST',
                success: function(res){
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)

                    if(res.code == 200){
                        alert(res.msg)
                        location.href="carousel_manage.html"
                    }
                        
                }
            })
        }

        function uploadCarousel(){
            $('#upload').change(function(event){
                
                let file = event.target.files[0]
                let formData = new FormData()

                formData.append('file', file)

                let upload = $.ajax({
                    data: formData,
                    url: baseUrl + 'upload.php',
                    type: 'POST',
                    contentType: false,
                    processData: false,
                })

                upload.then(res => {
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)

                    if(res.code == 200){
                        let imgUrl = res.data.imgUrl

                        let data = {
                            image : imgUrl
                        }

                        data = JSON.stringify(data)

                        return $.ajax({
                            data,
                            url : baseUrl + 'add_carousel.php',
                            type: 'POST',
                            
                        })
                    }

                }).then(res => {
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)

                    if(res.code == 200){
                        alert("上传轮播图成功")
                        location.reload()
                    }else{
                        alert("上传失败")
                    }
                })
            })
        }

        function renderCarousel(data) {
            for (let i = 0; i < data.length; i++) {
                $('.main .carousel-indicators').append(`
                    <li data-target="#carouselExampleIndicators" data-slide-to="${i}"></li>
                `)

                $('.main .carousel-inner').append(`
                    <div class="carousel-item">
                        <img src="${baseUrl + data[i].image}" class="d-block w-100" alt="...">
                    </div>
                `)

                $('.main .carouselBox').append(`
                    <div class="carousel">
                        <div class="carouselImg">
                            <img src="${baseUrl + data[i].image}">
                        </div>

                        <div class="operationBox">
                            <div class="button" onclick="delCarousel('${data[i].id}')">删除</div>
                        </div>    
                    </div>
                `)
            }

            $('.main .carousel-indicators').children().eq(0).addClass('active')
            $('.main .carousel-inner').children().eq(0).addClass('active')

        }
    </script>
</body>

</html>